package com.itlong.cloud.sdk.service.impl;

import com.itlong.cloud.POJO.VO.sdk.old.SDKAppOldUserBasicsInfoVO;
import com.itlong.cloud.POJO.VO.sdk.old.SDKAppOldUserInfoCacheVO;
import com.itlong.cloud.sdk.dao.ISDKAppOldUserDao;
import com.itlong.cloud.sdk.service.ISDKAppOldUserService;
import com.itlong.cloud.service.RedisService;
import com.itlong.cloud.utils.date.DateUtil;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class SDKAppOldUserServiceImpl implements ISDKAppOldUserService {



    //日志记录对象
    private static final Logger LOG = LoggerFactory.getLogger(SDKAppOldUserServiceImpl.class);

    @Autowired
    RedisService<SDKAppOldUserInfoCacheVO> sdkAppUserRedisService;  //缓存服务接口

    @Autowired
    ISDKAppOldUserDao isdkAppUserDao; //旧版SDK_APP用户数据访问接口

    /**
     * <desc>
     * 从缓存中获取SDK登录用户的信息.
     * </desc>
     *
     * @param appid   应用接入凭证
     * @param phoneNo 用户手机号
     * @return 手机APP SDK用户数据传输对象
     * @author LewisLouis
     * @createDate 2017-11-08
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public SDKAppOldUserInfoCacheVO getSDKAppCacheUser(String appid, String phoneNo) {

        String k = String.format("sdk_%s_%s", appid, phoneNo);
        SDKAppOldUserInfoCacheVO sdkAppUserInfoCacheVO = sdkAppUserRedisService.getObject(k, SDKAppOldUserInfoCacheVO.class);
        this.saveSDKAppCacheUser(sdkAppUserInfoCacheVO);
        return sdkAppUserInfoCacheVO;
    }

    /**
     * <desc>
     * 保存SDK登录的用户信息到缓存.
     * </desc>
     *
     * @param sdkAppUserVO 手机APP SDK用户数据传输对象
     * @author LewisLouis
     * @createDate 2017-11-08
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public void saveSDKAppCacheUser(SDKAppOldUserInfoCacheVO sdkAppUserVO) {
        if (null == sdkAppUserVO) {
            return;
        }
        Date now = new Date();
        sdkAppUserVO.setExpiresIn(7 * 24 * 60 * 60);
        sdkAppUserVO.setCreateTime(DateUtil.fomatDate(now, "yyyy-MM-dd HH:mm:ss"));
        sdkAppUserVO.setInvalidTime((DateUtil.fomatDate(DateUtil.addCurrentTime(7), "yyyy-MM-dd HH:mm:ss")));
        String k = String.format("sdk_%s_%s", sdkAppUserVO.getAppid(), sdkAppUserVO.getPhoneNo());
        sdkAppUserRedisService.set(k, sdkAppUserVO.getExpiresIn(), sdkAppUserVO);
    }

    /**
     * <desc>
     * 从数据库中获取SDK登录用户的信息.
     * </desc>
     *
     * @param phoneNo 用户手机号
     * @return 手机APP SDK用户数据传输对象
     * @author LewisLouis
     * @createDate 2017-11-08
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public SDKAppOldUserBasicsInfoVO getSDKAppUserBasicInfo(String phoneNo) {

        SDKAppOldUserBasicsInfoVO sdkAppOldUserBasicInfo = isdkAppUserDao.getSDKAppUserBasicInfo(phoneNo);
        return sdkAppOldUserBasicInfo;
    }

    /**
     * <desc>
     * 根据手机号和应用接入凭证，查找对应的项目信息.
     * </desc>
     *
     * @param phoneNo 手机号
     * @param appId   应用接入凭证
     * @return xx
     * @author LewisLouis
     * @createDate 2017-11-14
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public List<String> getProjectInfos(String phoneNo, String appId) {
        //获取手机号关联的项目信息
       List<String> projectsRelationPhoneNo = isdkAppUserDao.getProjectInfosByPhoneNo(phoneNo);
        //获取应用接入凭证关联的项目信息
       List<String> projectsRelationAppId = isdkAppUserDao.getProjectInfosByAppId(appId);
       //求两者交集
       projectsRelationAppId.retainAll(projectsRelationPhoneNo);
       return projectsRelationAppId;
    }

    /**
     * <desc>
     * 获取指定用户的所在房间的其中一个业主的电话号码
     * </desc>
     *
     * @param userId
     * @return 业主电话号码
     * @author LewisLouis
     * @createDate 2017-11-15
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public String getOneParentPhoneNoByUserId(String userId) {
        return isdkAppUserDao.getOneParentPhoneNoByUserId(userId);
    }

    /**
     * <desc>
     * 获取用户指定项目中的一卡通软卡号（系统编号）
     * </desc>
     *
     * @param userId    用户Id
     * @param projectId 项目Id
     * @return 一卡通软卡号（系统编号）.
     * @author LewisLouis
     * @createDate 2017-11-16
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public String getUserCardNo(String userId, String projectId) {
        return isdkAppUserDao.getUserCardNo(userId,projectId);
    }


}
